﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Animation;

namespace TotalApplication.AngelSix
{
    public static class StoryboardHelpers
    {
        #region Slide From/To Left

        public static void AddSlideFromLeft(this Storyboard storyboard, double offset, float slideSeconds, float decelerationRatio = 0.9f, bool keepMargin = true)
        {
            var slideAnimation = new ThicknessAnimation
            {
                From = new Thickness(-offset, 0, keepMargin ? offset : 0, 0),
                To = new Thickness(0),
                Duration = TimeSpan.FromSeconds(slideSeconds),
                DecelerationRatio = decelerationRatio
            };

            Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("Margin"));

            storyboard.Children.Add(slideAnimation);
        }

        public static void AddSlideToLeft(this Storyboard storyboard, double offset, float slideSeconds, float decelerationRatio = 0.9f, bool keepMargin = true)
        {
            var slideAnimation = new ThicknessAnimation
            {
                To = new Thickness(-offset, 0, keepMargin ? offset : 0, 0),
                From = new Thickness(0),
                Duration = TimeSpan.FromSeconds(slideSeconds),
                DecelerationRatio = decelerationRatio
            };

            Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("Margin"));

            storyboard.Children.Add(slideAnimation);
        }

        #endregion

        #region Slide From/To Right

        public static void AddSlideFromRight(this Storyboard storyboard, double offset, float slideSeconds, float decelerationRatio = 0.9f, bool keepMargin = true)
        {
            var slideAnimation = new ThicknessAnimation
            {
                From = new Thickness(keepMargin ? offset : 0, 0, -offset, 0),
                To = new Thickness(0),
                Duration = TimeSpan.FromSeconds(slideSeconds),
                DecelerationRatio = decelerationRatio
            };

            Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("Margin"));

            storyboard.Children.Add(slideAnimation);
        }


        public static void AddSlideToRight(this Storyboard storyboard, double offset, float slideSeconds, float decelerationRatio = 0.9f, bool keepMargin = true)
        {
            var slideAnimation = new ThicknessAnimation
            {
                To = new Thickness(keepMargin ? offset : 0, 0, -offset, 0),
                From = new Thickness(0),
                Duration = TimeSpan.FromSeconds(slideSeconds),
                DecelerationRatio = decelerationRatio
            };

            Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("Margin"));

            storyboard.Children.Add(slideAnimation);
        }

        #endregion

        #region Slide From/To Bottom

        public static void AddSlideFromBottom(this Storyboard storyboard, double offset, float slideSeconds, float decelerationRatio = 0.9f, bool keepMargin = true)
        {
            var slideAnimation = new ThicknessAnimation
            {
                From = new Thickness(0, keepMargin ? offset : 0, 0, -offset),
                To = new Thickness(0),
                Duration = TimeSpan.FromSeconds(slideSeconds),
                DecelerationRatio = decelerationRatio
            };

            Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("Margin"));

            storyboard.Children.Add(slideAnimation);
        }

        public static void AddSlideToBottom(this Storyboard storyboard, double offset, float slideSeconds, float decelerationRatio = 0.9f, bool keepMargin = true)
        {
            var slideAnimation = new ThicknessAnimation
            {
                To = new Thickness(0, keepMargin ? offset : 0, 0, -offset),
                From = new Thickness(0),
                Duration = TimeSpan.FromSeconds(slideSeconds),
                DecelerationRatio = decelerationRatio
            };

            Storyboard.SetTargetProperty(slideAnimation, new PropertyPath("Margin"));

            storyboard.Children.Add(slideAnimation);
        }

        #endregion

        #region  Fade In/Out Animation

        public static void AddFadeIn(this Storyboard storyboard, float seconds, bool from = false, double value = 1)
        {
            // Create the margin animate from right 
            var animation = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(seconds)),
                To = value,
            };

            // Animate from if requested
            if (from)
                animation.From = 0;

            // Set the target property name
            Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));

            // Add this to the storyboard
            storyboard.Children.Add(animation);
        }

        public static void AddFadeOut(this Storyboard storyboard, float seconds, bool from = false , double value = 1)
        {
            // Create the margin animate from right 
            var animation = new DoubleAnimation
            {
                Duration = new Duration(TimeSpan.FromSeconds(seconds)),
                From = value,
                To = 0,
            };

            // Animate from if requested
            if (from)
                animation.From = 0;

            // Set the target property name
            Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));

            // Add this to the storyboard
            storyboard.Children.Add(animation);
        }
        #endregion
    }
}
